﻿/*
 * ========================================================================
 * Copyright(c) 2008-2010北京高阳金信信息技术有限公司, All Rights Reserved.
 * ========================================================================
 *  模块/功能点的访问统计

 * 
 * 作者：邓太华     时间：2010-01-25
 * 版本：V1.0
 * 
 * 修改者：         时间：                
 * 修改说明：
 * ========================================================================
*/

using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using Hisuntech.SystemFramework.Common;
using PWMIS.DataProvider.Adapter ;
using System.Threading;

namespace Hisuntech.SystemFramework.Client.Action
{
    /// <summary>
    /// 模块/功能点的访问统计
    /// </summary>
    public class CountUse : Hisuntech.SystemFramework.Common.Interface.ICountUse
    {
        private static DateTime LastUploadTime = DateTime.Now;
       
      
        private DateTime _startTime;
        // 性能优化 操作队列 2009-09-13
      
        /// <summary>
        /// 统计开始时间
        /// </summary>
        public DateTime StartTime
        {
            get { return _startTime; }
            set { _startTime = value; }
        }
        private DateTime _endTime;
        /// <summary>
        /// 统计结束时间
        /// </summary>
        public DateTime EndTime
        {
            get { return _endTime; }
            set { _endTime = value; }
        }
        private int _ModuleCode;
        /// <summary>
        /// 模块/功能编码
        /// </summary>
        public int ModuleCode
        {
            get { return _ModuleCode; }
            set { _ModuleCode = value; }
        }

        private string _productNumber="";

        /// <summary>
        /// 序列号
        /// </summary>
        public string ProductNumber
        {
            get { return _productNumber; }
            set { _productNumber = value; }
        }

        private float _version;

        /// <summary>
        /// 版本号
        /// </summary>
        public float Version
        {
            get { return _version; }
            set { _version = value; }
        }


        /// <summary>
        /// 默认构造函数
        /// </summary>
        public CountUse()
        {
            this.StartTime = MyDateTime.GetSysDateTime();
        }

        /// <summary>
        /// 用模块编码初始化类
        /// </summary>
        /// <param name="moduleCode"></param>
        public CountUse(int moduleCode)
        {
            this.ModuleCode = moduleCode;
            this.StartTime = MyDateTime.GetSysDateTime();
           
        }
        // 性能优化 操作队列 2009-09-13
        /// <summary>
        /// 加访问统计加入到操作队列
        /// </summary>
        private void RealCount()
        {
            this.EndTime = MyDateTime.GetSysDateTime();
           
            TimeSpan ts = this.EndTime.Subtract(this.StartTime);

            SaveLocalData(this.ProductNumber, this.ModuleCode, this.StartTime, (int)ts.TotalSeconds, this.Version);

            this.StartTime = this.EndTime;
            ts = this.EndTime.Subtract(CountUse.LastUploadTime);
            if (ts.TotalHours > 1)
            {
                Thread thread = new Thread(new ThreadStart(UpLoadData));
                thread.Name = "CountUse_UpLoadData";
                thread.Start();
               
            }
        }
        /// <summary>
        /// 统计，如果本地数据库中最早的数据超过1小时且联网，那么将自动上传数据到服务器
        /// </summary>
        public void Count()
        {
            RealCount();
        }

        /// <summary>
        /// 保存数据到本地数据中
        /// </summary>
        /// <param name="productNumbers">序列号</param>
        /// <param name="moduleCode">模块编码</param>
        /// <param name="updateTime">访问时间</param>
        /// <param name="seconds">停留时间</param>
        /// <param name="version">版本</param>
        private void SaveLocalData(string productNumbers, int moduleCode, DateTime updateTime, int seconds, float version)
        {
            //苗青园 2009-6-13 添加
            string sql = @"
Insert into TB_Sys_Update(
ProductNumbers,
ModuleCode,
updateTime,
seconds,
version
)
Values
(
@ProductNumbers,
@ModuleCode,
@updateTime,
@seconds,
@version
)
";
            IDataParameter[] paras =
            {
                MyDB.Instance.GetParameter ("@ProductNumbers",productNumbers),// new SqlParameter ("@Email",SqlDbType.VarChar,50),
                MyDB.Instance.GetParameter("@ModuleCode",moduleCode),
                MyDB.Instance.GetParameter("@updateTime",updateTime),
                MyDB.Instance.GetParameter("@seconds",seconds),
                MyDB.Instance.GetParameter("@version",version),
            };

           
            int count = MyDB.Instance.ExecuteNonQuery (sql,CommandType.Text,paras);
           


        }

        /// <summary>
        /// 上传数据到服务器，用于手工上传，默认会自动调用该方法。
        /// </summary>
        /// <returns></returns>
        public void UpLoadData()
        {  //苗青园 2009-6-13 添加
        //    HisunTech.Common.BLL.MBRegedit.CSysUpdate cs = null;
        //    string message = null;
        //    string sql = "";

        //    IList<HisunTech.Common.BLL.MBRegedit.CSysUpdate> il = new List<HisunTech.Common.BLL.MBRegedit.CSysUpdate>();
        //    try
        //    {
        //        DataTable da = conn.GetDataTable("select * from  MB_Sys_Update ");


        //        cs = new HisunTech.Common.BLL.MBRegedit.CSysUpdate();
        //        if (da != null)
        //        {

        //            for (int j = 0; j < da.Rows.Count; j++)
        //            {
        //                cs.ProductNumbers = da.Rows[j]["ProductNumbers"].ToString();
        //                cs.Seconds = Convert.ToInt32(da.Rows[j]["seconds"].ToString());
        //                cs.Version = Convert.ToSingle(da.Rows[j]["version"].ToString());
        //                cs.UpdateTime = Convert.ToDateTime(da.Rows[j]["updateTime"]);
        //                cs.ModuleCode = Convert.ToInt32(da.Rows[j]["ModuleCode"].ToString());

        //                il.Add(cs);
        //            }


        //            // if (RemoteServer.ConnectedMBServer)
        //            if (netWork.ServerStatuses.Count != 0 && netWork.ServerStatuses["jjzd.url"] != ServerStatus.Disconnected)//wcp 2009-8-19
        //            {
        //                if (HisunTech.Common.BLL.RemoteServer.GetCAService().SaveUseCountData(il.ToArray(), out message))
        //                {
        //                    try
        //                    {
        //                        sql = "delete from MB_Sys_Update";
        //                        conn.TranBegin();
        //                        int count = conn.TranUpdate(sql);
        //                        conn.TranCommit();
        //                    }
        //                    catch (Exception e)
        //                    {
        //                        message = e.Message;
        //                        if (conn != null)
        //                        {
        //                            conn.Close();
        //                        }
        //                        //return false;
        //                    }
        //                    if (netWork.ServerStatuses.Count != 0 && netWork.ServerStatuses["jjzd.url"] != ServerStatus.Disconnected)//wcp 2009-8-19修改
        //                    {

        //                        LastUploadTime = MyDateTime.GetSysDateTime();
        //                    }
        //                    else
        //                    {
        //                        LastUploadTime = DateTime.Now;
        //                    }
        //                    // LastUploadTime = Config.GetSysDateTime();
        //                    //return true;
        //                }
        //                //上传成功数据以后必须设置 LastUploadTime
        //            }

        //        }
        //    }
        //    catch
        //    {

        //    }
        //    if (netWork.ServerStatuses.Count != 0 && netWork.ServerStatuses["jjzd.url"] != ServerStatus.Disconnected)//wcp 2009-8-19修改
        //    {
        //        LastUploadTime = MyDateTime.GetSysDateTime();
        //    }
        //    else
        //    {
        //        LastUploadTime = DateTime.Now;
        //    }
        //    // LastUploadTime = Config.GetSysDateTime();
        //    //test
        //    // return false;

        }
    }
}
